package Client;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import GroupClass.ConnectRMI;
import GroupClass.UnitOfUser;
import Project_etow.EtowInterface;

public class MyUnit extends javax.swing.JDialog {
	private static String chua = null;
	
    public MyUnit(java.awt.Frame parent, boolean modal,String getidfromprofiles) {
        super(parent, modal);
        chua = getidfromprofiles;
        initComponents();
    }

    ConnectRMI connectRMI = new ConnectRMI();

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {				
        jScrollPane1 = new javax.swing.JScrollPane();
        tbdulieu = new javax.swing.JTable();
        btnunit = new javax.swing.JButton();
        bbtnbill = new javax.swing.JButton();
        btnexit = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("My Unit");

        Object colNames[] = {"", "Town Name", "Floor Number", "Unit Name","Status Bill"};  
  	  	Object[][] data = {};  
  	  	DefaultTableModel dtm; 
  	  
  	  	dtm = new DefaultTableModel(data,colNames);  
  	  	tbdulieu = new JTable(dtm);  
  	  try {				 
          	    Registry registry = LocateRegistry.getRegistry(connectRMI.host, connectRMI.port);
                    EtowInterface obj = (EtowInterface) registry.lookup(connectRMI.regis);    
  			
  		List<UnitOfUser> x =obj.showunit(chua);
  			
  		
  		for(GroupClass.UnitOfUser d:x) {
        	String townname = d.getTownname();
        	String floorname = d.getFloornumber();
        	String unitname = d.getUnitname();
        	String statusbill = d.getStatusbill();
        	
        	
  	    dtm.addRow(new Object[]{new Boolean(false),townname,floorname,unitname,statusbill});
  		}									
  			
  		}catch (Exception ex) {
  			ex.printStackTrace();
  		}	  	    
  		
  		TableColumn tc = tbdulieu.getColumnModel().getColumn(0);  
  	    tc.setCellEditor(tbdulieu.getDefaultEditor(Boolean.class));  
  	    tc.setCellRenderer(tbdulieu.getDefaultRenderer(Boolean.class));  
  	    tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
  	    
  	  	
        jScrollPane1.setViewportView(tbdulieu);

        btnunit.setText("View Detail Unit");
        btnunit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnunitActionPerformed(evt);
            }
        });

        bbtnbill.setText("View Detail Bill");
        bbtnbill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bbtnbillActionPerformed(evt);
            }
        });

        btnexit.setText("Exit");
        btnexit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnexitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(btnunit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bbtnbill)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
                .addComponent(btnexit)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnunit)
                    .addComponent(bbtnbill)
                    .addComponent(btnexit))
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>

    private void btnunitActionPerformed(java.awt.event.ActionEvent evt) {
    	
    }

    private void bbtnbillActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void btnexitActionPerformed(java.awt.event.ActionEvent evt) {
        setVisible(false);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MyUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MyUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MyUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MyUnit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MyUnit dialog = new MyUnit(new javax.swing.JFrame(), true,chua);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton bbtnbill;
    private javax.swing.JButton btnexit;
    private javax.swing.JButton btnunit;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbdulieu;
    class MyItemListener implements ItemListener  
    {  
     public void itemStateChanged(ItemEvent e) {  
        Object source = e.getSource();  
        if (source instanceof AbstractButton == false) return;  
        boolean checked = e.getStateChange() == ItemEvent.SELECTED;  
        for(int x = 0, y = tbdulieu.getRowCount(); x < y; x++)  
        {  
        	tbdulieu.setValueAt(new Boolean(checked),x,0);  
        }  
      }  
    }

}

class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {  
	
	protected CheckBoxHeader rendererComponent;  
	protected int column;  
	protected boolean mousePressed = false;  
	
	public CheckBoxHeader(ItemListener itemListener) {  
	rendererComponent = this;  
	rendererComponent.addItemListener(itemListener);  
	}  
	public Component getTableCellRendererComponent(  
	  JTable table, Object value,  
	  boolean isSelected, boolean hasFocus, int row, int column) {  
	  if (table != null) {  
	  JTableHeader header = table.getTableHeader();  
	  if (header != null) {  
	    rendererComponent.setForeground(header.getForeground());  
	    rendererComponent.setBackground(header.getBackground());  
	    rendererComponent.setFont(header.getFont());  
	    header.addMouseListener(rendererComponent);  
	  }  
	}  
	setColumn(column);  
	rendererComponent.setText("Check All");  
	setBorder(UIManager.getBorder("TableHeader.cellBorder"));  
	return rendererComponent;  
	}  
	protected void setColumn(int column) {  
	this.column = column;  
	}  
	public int getColumn() {  
	return column;  
	}  
	protected void handleClickEvent(MouseEvent e) {  
	if (mousePressed) {  
	  mousePressed=false;  
	  JTableHeader header = (JTableHeader)(e.getSource());  
	  JTable tableView = header.getTable();  
	  TableColumnModel columnModel = tableView.getColumnModel();  
	  int viewColumn = columnModel.getColumnIndexAtX(e.getX());  
	  int column = tableView.convertColumnIndexToModel(viewColumn);  
	
	  if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {  
	    doClick();  
	  }  
	}  
	}  
	public void mouseClicked(MouseEvent e) {  
	handleClickEvent(e);  
	((JTableHeader)e.getSource()).repaint();  
	}  
	public void mousePressed(MouseEvent e) {  
	mousePressed = true;  
	}  
	public void mouseReleased(MouseEvent e) {  
	}  
	public void mouseEntered(MouseEvent e) {  
	}  
	public void mouseExited(MouseEvent e) {  
	}  
}  

